import crypto from 'crypto';

export const md5 = (str: string) => {
    const md5 = crypto.createHash('md5');
    md5.update(str);
    return md5.digest('hex');
};

function x(e, w?, c?) {
    var t = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : 2, r = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : "0", n = arguments.length > 3 && void 0 !== arguments[3] && arguments[3];
    return e = e.toString(), e.length >= t ? e : n ? e + r.repeat(t - e.length) : r.repeat(t - e.length) + e;
}

function T(e) {
    for (var t: any = [], r = [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" ], n = 0; n < e; n++) {
        var i = Math.floor(10 * Math.random());
        t[n] = r[i];
    }
    return t.join("");
}

function k(e, w?) {
    var t: any = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : 2, r = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : "0";
    return x(e, t, r);
}

export const getSu = function() {
    var e = new Date(), t = e.getFullYear(), r = k(e.getMonth() + 1), n = k(e.getDate()), i = k(e.getHours()), a = k(e.getMinutes()), o = k(e.getSeconds()), u = k(e.getMilliseconds(), 3);
                return "".concat(String(t).slice(2, 4)).concat(r).concat(n).concat(i).concat(a).concat(o).concat(u, "1000000").concat(T(10));
}

// function n(e, t?) {
//     if (e) {
//         if ("string" === typeof e) return d(e, t);
//         var r = Object.prototype.toString.call(e).slice(8, -1);
//         return "Object" === r && e.constructor && (r = e.constructor.name), "Map" === r || "Set" === r ? Array.from(e) : "Arguments" === r || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r) ? n(e, t) : void 0;
//     }
// }

function a(e, t?) {
    var r = "undefined" !== typeof Symbol && e[Symbol.iterator] || e["@@iterator"];
    // if (!r) {
    //     if (Array.isArray(e) || (r = n(e)) || t && e && "number" === typeof e.length) {
    //         r && (e = r);
    //         var i = 0, a = function() {};
    //         return {
    //             s: a,
    //             n: function() {
    //                 return i >= e.length ? {
    //                     done: !0
    //                 } : {
    //                     done: !1,
    //                     value: e[i++]
    //                 };
    //             },
    //             e: function(e) {
    //                 throw e;
    //             },
    //             f: a
    //         };
    //     }
    //     throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
    // }
    var o, u = !0, s = !1;
    return {
        s: function() {
            r = r.call(e);
        },
        n: function() {
            var e = r.next();
            return u = e.done, e;
        },
        e: function(e) {
            s = !0, o = e;
        },
        f: function() {
            try {
                u || null == r["return"] || r["return"]();
            } finally {
                if (s) throw o;
            }
        }
    };
}

export const getSign = (signData= {}) => {
    var e, t = signData, r = "qapi.huolala.cn", n = Object.keys(t).sort(), o = a(n);
    let i: string[] = [];
    try {
        for (o.s(); !(e = o.n()).done; ) {
            var u = e.value;
            i.push("".concat(u).concat(t[u]));
        }
    } catch (s) {
        // s = VM2_INTERNAL_STATE_DO_NOT_USE_OR_PROGRAM_WILL_FAIL.handleException(s);
        o.e(s);
    } finally {
        o.f();
    }
    return md5("".concat(i.join("")).concat(r));
}

function D() {
    return false;
    // return c?.oApi?.oGlobal?._pluginsSignature?.decode;
}

export const kvStringify = (e, t, r) => {
    const sEnv: string = "WEB";
    let s: any[] = [];
    function n(e) {
        return '[object Array]' === Object.prototype.toString.call(e);
    }
    function a(e, t) {
        var r, n;
        if (typeof t == "undefined" || "undefined" == t || t == "null" || null == t) if (e == "post_body") s[s.length] = e + "=" + t; else if (t = null, 
        typeof D() == "undefined" || !D() || sEnv != "WEAPP" && sEnv != "ALIPAY") s[s.length] = e + "="; else try {
            s[s.length] = decodeURIComponent(e) + "=";
        } catch (o) {
            // o = VM2_INTERNAL_STATE_DO_NOT_USE_OR_PROGRAM_WILL_FAIL.handleException(o);
            console.log('VM2_INTERNAL_STATE_DO_NOT_USE_OR_PROGRAM_WILL_FAIL')
            s[s["length"]] = e + "=";
        } else if (e == "post_body") s[s.length] = e + "=" + t; else if (typeof D() == "undefined" || !D() || "WEAPP" != sEnv && sEnv !== "ALIPAY") s[s.length] = e + "=" + t; else {
            try {
                r = e, r = decodeURIComponent(e);
            } catch (l) {
                console.log('VM2_INTERNAL_STATE_DO_NOT_USE_OR_PROGRAM_WILL_FAIL')
                // l = VM2_INTERNAL_STATE_DO_NOT_USE_OR_PROGRAM_WILL_FAIL.handleException(l);
                r = e;
            }
            try {
                n = t, n = decodeURIComponent(t);
            } catch (d) {
                console.log('VM2_INTERNAL_STATE_DO_NOT_USE_OR_PROGRAM_WILL_FAIL')
                // d = VM2_INTERNAL_STATE_DO_NOT_USE_OR_PROGRAM_WILL_FAIL.handleException(d);
                n = t;
            }
            s[s.length] = r + "=" + n;
        }
    }
    return function e(t, r) {
        // console.log('kvStringify', t);
        // console.log('kvStringify', r);
        var i, o, c;
        if (t) n(r) || r && String(r) === "[object Object]" ? a(t, JSON.stringify(r)) : a(t, r); else if (n(r)) for (i = 0, 
        o = r.length; i < o; i++) a(r[i].name, r[i].value); else for (c in r) e(c, r[c]);
        return s;
    }("", e).join("&");
}